package com.itheima.controller;

import com.itheima.domain.Goods;
import com.itheima.service.GoodsService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Administrator
 */

@RestController
@RequestMapping("/goods")
public class GoodsController {


    @Autowired
    private GoodsService service;


    /**
     * 降级：
     * 1. 出现异常
     * 2. 服务调用超时
     * * 默认1s超时
     *
     * @HystrixCommand(fallbackMethod = "findOne_fallback",commandProperties={})
     * fallbackMethod：指定降级后调用的方法名称
     * <p>
     * commandProperties:设置熔断器默认配置
     * fallbackMethod 设置服务降级处理方法 提供方
     */
    @GetMapping("/findOne/{id}")
    @HystrixCommand(fallbackMethod = "findOne_fallback", commandProperties = {
            //execution.isolation.thread.timeoutInMilliseconds 设置Hystrix熔断器的超时时间，默认1s  设为3000毫秒
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
    })
    public Goods findOne(@PathVariable int id) {

        //构造异常
        if (id == 2) {
            int i = 1 / 0;
        }


        return service.findOne(id);
    }

    /**
     * 定义降级方法：
     * 1. 方法的返回值需要和调用方法一样
     * 2. 方法的参数（个数和类型）需要和调用方法一样
     */
    public Goods findOne_fallback(int id) {
        Goods goods = new Goods();
        goods.setTitle("系统维护中~~~");

        return goods;
    }

}
